<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['ischecklogin']=$this->model('member')->get_member();
set_html('title','地址管理');
if($_RQ['op']=='list'){
	if($_RQ['type']){
		$this->session('type',$_RQ['type']);
	}
	//读取收货地址列表
	$list=pdo_getall('jy_ailiao_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'type'=>$this->session('type')));
	if($list){
		foreach($list as &$item){
			$item['pcd']=@json_decode($item['pcd'],true);
			if($item['pcd']){
				$item=array_merge($item['pcd'],$item);
			}
		}
	}
	if($_RQ['choose']){
		$return['choose']=$_RQ['choose'];
	}
	$return['type']=$this->session('type');
	$return['list']=$list;
	$return['goback']=$this->session('goback')?:'jy_ailiao/index/';
	exi($return);
}
if($_RQ['op']=='edit'){
	$return['type']=$this->session('type');
	//添加，编辑收货地址
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_ailiao_address',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('province'=>'');
	}else{
		$item=array('pcd'=>array('province'=>''));
	}
	$return['item']=$item;
	$return['wxapp_id']=$_SESSION['wxapp_id']?:0;
	$return['openid']=$_SESSION['openid'];
	$return['province']=cfc('tools')->pcd();
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!preg_match("/^1[3456789]{1}\d{9}$/",$postdata['mobile'])){
		exi('手机号码填写错误！','error');
	}
	if(!$postdata['name']){
		exi('请填写姓名！','error');
	}
	if(!$postdata['pcd']){
		exi('请填写区域！','error');
	}
	if(!$postdata['address']){
		exi('请填写地址详情！','error');
	}
	$data=array(
		'isdefault'=>1,
		'mobile'=>trim($postdata['mobile']),
		'name'=>trim($postdata['name']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'lat_lng'=>ijson_encode(array('lng'=>'')),
		'address'=>trim($postdata['address'])
	);
	pdo_update('jy_ailiao_address',array('isdefault'=>0),array('mid'=>$_SESSION['uid'],'type'=>$this->session('type'),'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_ailiao_address',$data,array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$addressid = $id;
	}else{
		$data['mid']=$_SESSION['uid'];
		$data['uniacid']=$_SESSION['uniacid'];
		$data['type']=$this->session('type');
		$data['createtime']=time();
		pdo_insert('jy_ailiao_address',$data);
		$addressid = pdo_insertid();
	}
	if(($postdata['res']==1 && $addressid) || $postdata['get']==1){
		exi($addressid);
	}else{
		exi('设置成功！','','jy_ailiao/member.address/');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的地址','error');
	}
	pdo_update('jy_ailiao_address',array('isdefault'=>0),array('mid'=>$_SESSION['uid'],'type'=>$this->session('type'),'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	pdo_update('jy_ailiao_address',array('isdefault'=>1),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($_RQ['get']==1){
		exi('success');
	}else{
		exi('','','goback');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要删除的地址','error');
	}
	$isorder=pdo_getcolumn('jy_weishop_order',array('addressid'=>$id,'uniacid'=>$_SESSION['uniacid']),'id');
	if($isorder){
		exi('该地址已经用于订单收货，不能删除！','error');
	}
	pdo_delete('jy_ailiao_address',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($_RQ['get']==1){
		exi('success');
	}else{
		exi('','','jy_ailiao/member.address/');
	}
}
if($_RQ['op']=='choose'){	
	exi($return);
}